package qdu.my.ch2_service;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.IBinder;

public class MusicService extends Service{
	static boolean isplay;
	MediaPlayer player;
	Music_binder mb = new Music_binder();
	class Music_binder extends Binder{
		public void startmusic(){
			if(!player.isPlaying()){
				player.start();
				isplay=player.isPlaying();
				System.out.println("当前播放状态 isplay: "+ isplay);
			}
		}
		public void stopmusic(){
			if(player.isPlaying()){
			    player.pause();
			    isplay=player.isPlaying();
			    System.out.println("播放状态 isplay: " + isplay);
			}//player.release();
		}
	}
	@Override
	public IBinder onBind(Intent intent) {
		System.out.println("绑定模式onbind");
		return null;//mb;
	}
	
	
	@Override
	public boolean onUnbind(Intent intent) {
//		System.out.println("解除绑定");
//		if(player != null)
//			player.stop();
//		    player.release();
		return super.onUnbind(intent);
	}


	@Override
	public void onCreate() {
		//MediaPlayer
		player=MediaPlayer.create(this, R.raw.main);
		super.onCreate();
		System.out.println("创建媒体播放对象");
	}
	
	//
	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		if(!player.isPlaying()){
			player.start();//
			isplay=player.isPlaying();//
			System.out.println("onstart, 非绑定");
		}
		return super.onStartCommand(intent, flags, startId);
	}
	//
	@Override
	public void onDestroy() {
		player.stop();
		isplay=player.isPlaying();//
		player.release();//
		super.onDestroy();
		System.out.println("停止并销毁媒体对象");
	}
}
